﻿// Copyright richyp 2009
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Richyp.Bodger.UI {
    public partial class ToggleWargearControl : UserControl {
        public ToggleWargearControl(TabPage page) {
            InitializeComponent();
            this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer,true);
            this.TabPage = page;
        }

        bool populating = false;
        public TabPage TabPage { get; set; }
        public Unit Unit { get; set; }
        public ListEntry ListEntry { get; set; }
        public Wargear Wargear { get; set; }
        public void Populate() {
            populating = true;
            int count = 0;
            foreach (Unit unit in this.ListEntry.Units) {
                if (unit.Name == this.Unit.Name) {
                    count++;
                }
            }
            this.checkBoxEquipped.Text = Wargear.Name;
            foreach (Equipment option in Unit.OptionalEquipment) {
                if (option.Wargear.Name == Wargear.Name) {
                    if (option.MaximumPerSquad < this.ListEntry.Units.Count && option.MaximumPerSquad > 0) {
                        this.labelCost.Text = (option.MaximumPerSquad * option.Cost).ToString();
                    } else {
                        this.labelCost.Text = (count * option.Cost).ToString();
                    }
                }
            }
            foreach (Equipment equipment in Unit.Equipped) {
                if (equipment.Wargear.Name == Wargear.Name) {
                    this.checkBoxEquipped.Checked = true;
                    break;
                }
            }
            populating = false;
        }

        private void checkBoxEquipped_CheckedChanged(object sender, EventArgs e) {
            if (!populating) {
                if (!this.checkBoxEquipped.Checked) {
                    foreach (Unit unit in this.ListEntry.Units) {
                        if (unit.Name == this.Unit.Name) {
                            Equipment eq = null;
                            foreach (Equipment equipment in unit.Equipped) {
                                if (equipment.Wargear.Name == Wargear.Name) {
                                    eq = equipment;
                                    break;
                                }
                            }
                            if (eq != null) {
                                unit.Equipped.Remove(eq);
                            }
                        }
                    }
                    this.ListEntry.UpdateRow();
                } else {
                    int count = 0;
                    foreach (Unit unit in this.ListEntry.Units) {
                        if (unit.Name == this.Unit.Name) {
                            Equipment eq = null;
                            foreach (Equipment equipment in unit.OptionalEquipment) {
                                if (equipment.Wargear.Name == Wargear.Name) {
                                    eq = equipment;
                                    break;
                                }
                            }
                            if (eq != null) {
                                Equipment copy = new Equipment(Wargear, eq.Cost);
                                copy.Default = eq.Default;
                                copy.Fixed = eq.Fixed;
                                copy.MaximumPerSquad = eq.MaximumPerSquad;
                                copy.Toggle = eq.Toggle;
                                copy.Wargear = eq.Wargear;
                                copy.Removes = eq.Removes;
                                unit.Equipped.Add(copy);

                                if (copy.Removes != null && copy.Removes.Length > 0) {
                                    string[] items = copy.Removes.Split(new char[] { ',' });
                                    List<Equipment> remove = new List<Equipment>();
                                    foreach (string item in items) {                                       
                                        foreach (Equipment eqr in unit.Equipped) {
                                            if (eqr.Wargear.Name.ToUpper() == item.ToUpper()) {
                                                remove.Add(eqr);
                                                break;
                                            }
                                        }
                                    }
                                    foreach (Equipment eqr in remove) {
                                        unit.Equipped.Remove(eqr);
                                    }
                                }
                                count++;
                                if (count >= eq.MaximumPerSquad) {
                                    break;
                                }
                            }
                        }
                    }
                    this.ListEntry.UpdateRow();
                }
            }

        }
    }
}
